package cn.edu.zzuli.admin.controller;

import cn.edu.zzuli.admin.service.IdentityService;
import cn.edu.zzuli.common.result.Result;
import cn.edu.zzuli.common.result.ResultInfo;
import cn.edu.zzuli.model.entity.Identity;
import cn.edu.zzuli.model.vo.identity.IdentityQueryVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


@RestController
@Api("后台管理系统api")
@RequestMapping("/background/admin/identity")
public class ApiIdentityController{

    @Autowired
    private IdentityService identityService;


    @ApiOperation("获取学生身份信息列表")
    @GetMapping("findAllIdentity")
    public ResultInfo findAllIdentity(){
        List<Identity> list = identityService.list();
        return Result.ok(list);
    }


    @ApiOperation("根据分页参数以查询列表")
    @GetMapping("list/{page}/{limit}")
    public ResultInfo list(@PathVariable Integer page,
                           @PathVariable Integer limit){
        Page<Identity> identityPage = identityService.findAllByPage(page,limit);
        return Result.ok(identityPage);
    }

    @ApiOperation("根据分页参数以及选择的条件查询列表")
    @GetMapping("chooseList/{page}/{limit}")
    public ResultInfo list(@PathVariable Integer page,
                           @PathVariable Integer limit,
                           IdentityQueryVo identityQueryVo){
        Page<Identity> identityPage = identityService.findAllByCond(page,limit,identityQueryVo);
        return Result.ok(identityPage);
    }


    @CacheEvict(value = "identity", allEntries=true)
    @ApiOperation("增加一位学生")
    @PostMapping("addStudent")
    public ResultInfo addStudent(@RequestBody Identity identity){
        boolean save = identityService.save(identity);
        return Result.ok(save);
    }

    @CacheEvict(value = "identity", allEntries=true)
    @ApiOperation("逻辑删除学生信息")
    @DeleteMapping("delete/{id}")
    public ResultInfo deleteStudent(@PathVariable Integer id){
        boolean res = identityService.removeById(id);
        return res == true ? Result.ok() : Result.fail("删除失败");
    }

    @ApiOperation("通过excel导入学生数据")
    @PostMapping("importData")
    public ResultInfo importData(MultipartFile file){
        identityService.importStudentData(file);
        return Result.ok();
    }

    @ApiOperation("导出学生列表为excel")
    @GetMapping("exportData")
    public void exportData(HttpServletResponse response){
        identityService.exportIdentityData(response);
    }


    @CacheEvict(value = "identity", allEntries=true)
    @ApiOperation("批量删除学生信息")
    @DeleteMapping("batchRemove")
    public ResultInfo batchRemove(@RequestBody List<Integer> idList){
        boolean flag = identityService.removeByIds(idList);
        return flag ? Result.ok("成功") : Result.fail("删除失败");
    }


    @CacheEvict(value = "identity", allEntries=true)
    @ApiOperation("修改一名学生信息")
    @PostMapping("updateStudent")
    public ResultInfo updateStudent(@RequestBody Identity identity){
        boolean flag = identityService.updateById(identity);
        return flag ? Result.ok("成功"): Result.fail("修改失败");
    }

    @ApiOperation("获取一位学生信息")
    @GetMapping("getInfo/{id}")
    public ResultInfo getInfo(@PathVariable Integer id){
        Identity identity = identityService.getById(id);
        return Result.ok(identity);
    }

    @ApiOperation("查询所有被删除的学生信息")
    @GetMapping("getDeletedInf")
    public ResultInfo getDeletedInf(IdentityQueryVo dentityQueryVo){
        List<Identity> list = identityService.getDeletedInf(dentityQueryVo.getName());
        return Result.ok(list);
    }

    @CacheEvict(value = "identity", allEntries=true)
    @ApiOperation("撤销删除")
    @GetMapping("backOut/{id}")
    public ResultInfo backOut(@PathVariable Integer id){
        identityService.backOut(id);
        return Result.ok();
    }
}
